﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuMianChaZhi
{
    class DrawOnControl
    {
        public static string[] colors = {"006837", "1a9850", "66bd63", "a6d96a", "d9ef8b", "ffffbf", "fee08b", "fdae61", "f46d43", "d73027", "a50026"};

        public static Color HexToColor(string hex)
        {
            byte br = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            byte bg = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
            byte bb = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
            //byte cc = byte.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
            int r = br;
            int g = bg;
            int b = bb;
            //int a = cc / 255;
            return Color.FromArgb(r, g, b);
        }

        public static Color RainbowNumberToColor(float number)
        {
            byte r = 0, g = 0, b = 0;
            if (number < 1 / 6f)
            {
                // Mostly red with some green.
                r = 255;
                g = (byte)(r * (number - 0) / (2 / 6f - number));
            }
            else if (number < 2 / 6f)
            {
                // Mostly green with some red.
                g = 255;
                r = (byte)(g * (2 / 6f - number) / (number - 0));
            }
            else if (number < 3 / 6f)
            {
                // Mostly green with some blue.
                g = 255;
                b = (byte)(g * (2 / 6f - number) / (number - 4 / 6f));
            }
            else if (number < 4 / 6f)
            {
                // Mostly blue with some green.
                b = 255;
                g = (byte)(b * (number - 4 / 6f) / (2 / 6f - number));
            }
            else if (number < 5 / 6f)
            {
                // Mostly blue with some red.
                b = 255;
                r = (byte)(b * (4 / 6f - number) / (number - 1f));
            }
            else
            {
                // Mostly red with some blue.
                r = 255;
                b = (byte)(r * (number - 1f) / (4 / 6f - number));
            }
            return Color.FromArgb(r, g, b);
        }

        public static void Draw(Graphics g, ReadInTemp ReadInData)
        {

            if (ReadInData == null || ReadInData.ValueCount == null)
            {
                return;
            }

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            double zMin = ReadInData.Min;
            double zMax = ReadInData.Max;
   


            //double colourOffset = xMax - xMin;

            int canvasWidth = 500;
            int canvasHeight = 500;

            int n = 80;
            int m = 80;


            int wx = canvasWidth / n;
            int wy = canvasHeight / m;

            for (int x = 0; x < n; x += 1)
            {
                for (int y = 0; y < m; y += 1)
                {

                    double xi = x - 40;
                    double yi = y - 40;

                    if (xi * xi + yi * yi > 40 * 40)
                    {
                        continue;
                    }

                    double ptelement = ReadInData.RawData[x][y];

                    double z = ((ptelement - zMin) / (zMax - zMin));

                    if (z < 0.0) z = 0.0;
                    if (z > 1) z = 1;
      

                    int rx = x * 5;
                    int ry = y * 5;

                    int indexColor = (int)Math.Floor((colors.Length - 1) * z);
                    Color clr = HexToColor(colors[indexColor]);

                    //Color clr = RainbowNumberToColor((float)z);

                    Brush brsh = new SolidBrush(clr);
                    g.FillEllipse(brsh, rx - 4, ry - 4, 8, 8);
                }
            }
        }
    }
}
